package sg.edu.nus.iss.billing.parse;
/**
 * @author sammil
 *
 */
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;

public class PaymentHistoryState extends ParserState {
	
	private static final int MAX_LENGTH = 4 * Parser.MAX_LINE_LEN;
	public static final PaymentHistoryState SINGLETON = new PaymentHistoryState();

	private PaymentHistoryState() {

	}

	@Override
	public <L> ParserState parse(LineNumberReader in, HashMap<String,ArrayList<L>> outputMap) throws IOException {

		System.out.println("In PaymentHistoryState at line " + in.getLineNumber());
		
		if (!PaymentHeaderParser.SINGLETON.parse(in, outputMap)) {
			
			throw createException("Expected PaymentHeaderParser header", in);
		}
		
		while (true) {
			in.mark(MAX_LENGTH);
			
			// try to parse a PaymentHistory record
			//System.out.println("################try to parse a PaymentHistory record");
			if (!PaymentHistoryParser.SINGLETON.parse(in, outputMap)) {
				
				
				// If we don't get a PaymentHistoryParser then try to parse
				// AccountHeaderParser
				if (!AccountHeaderParser.SINGLETON.parse(in, outputMap)){
					// If we don't get a AccountHeaderParser then try to parse
					// PaymentHeaderParser 
	
					if (!PaymentHeaderParser.SINGLETON.parse(in, outputMap)) {
						
						// If that didn't work then we must have finished
						return FinishedState.SINGLETON;
					}
				}		
			}
		}
		
	}

}
